﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Masters/AdminMasterPage.master" AutoEventWireup="true" CodeFile="ProductManager.aspx.cs" Inherits="Admin_ProductManager" %>
<%@ Import Namespace="System.Linq.Dynamic" %>
<%@ Register src="Parts/Pagging.ascx" tagname="Pagging" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Title" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
    <script type="text/javascript">
        $(document).ready(function () {

            $('a[name="deleteone"]').click(function () {
                if (confirm('Bạn có chắc muốn xóa tài liệu này không?') == false) {
                    return false;
                }
            });

            $('#btndelete').click(function () {
                if ($('input[name="MaTaiLieu"]:checked').length <= 0) {
                    alert("Bạn chưa chọn tài liệu cần xóa.");
                    return false;
                } else {
                    if (confirm('Bạn có chắc muốn xóa những tài liệu được chọn không?') == false) {
                        return false;
                    } else {
                        $('#product_manager').attr('action', $('#deletehidden').val());
                        $(this).submit();
                    }

                }


            });
        });

</script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">

<div class="content-box"><!-- Start Content Box -->
				
		<div class="content-box-header">
					
			<h3>Quản lý Tài Liệu</h3>
					
			<ul class="content-box-tabs">
				<li><a href="#tab1" class="default-tab">Danh sách</a></li>                
			</ul>
					
			<div class="clear"></div>
					
		</div> <!-- End .content-box-header -->
				
<div class="content-box-content">
  
<!-- Start Tab Danh Sach-->
<%
	          
               
              string act = "";
              int tlId =0;
              bool isSuccess = false;
              
              if (Request.QueryString["act"] != null)
                  act = Request.QueryString["act"].ToString();
              if (Request.QueryString["id"] != null)
                  tlId = Convert.ToInt32(Request.QueryString["id"].ToString());
                               
    
              NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();
             
              if (act != "" && act == "delete")
              {
                  if (tlId > 0)
                  {
                      //xóa 1
                      if ((from tl in db.TaiLieus
                           where tl.ProductID.Equals(tlId)
                           select tl).Count() > 0)
                      {
                          var querytl = (from tl in db.TaiLieus
                                         where tl.ProductID.Equals(tlId)
                                         select tl).Single();
                         
                          db.TaiLieus.DeleteOnSubmit(querytl);
                          db.SubmitChanges();
                          isSuccess = true;
                         
                      }
                  }
                  else {
                      string MaKhachHangTemp = Request.Form["MaTaiLieu"];
                      if (MaKhachHangTemp != null)
                      {
                          string[] MaKhachHangs = MaKhachHangTemp.Split(',');
                          foreach (string item in MaKhachHangs)
                          {
                              int ma = Convert.ToInt32(item.Trim());
                              if ((from tl in db.TaiLieus
                                   where tl.ProductID.Equals(ma)
                                   select tl).Count() > 0)
                              {
                                  var querytl = (from tl in db.TaiLieus
                                                 where tl.ProductID.Equals(ma)
                                                 select tl).Single();

                                  db.TaiLieus.DeleteOnSubmit(querytl);
                                  db.SubmitChanges();
                                  isSuccess = true;

                              }
                          }
                         
                      }
                  }
              }



              StringBuilder whereQuery = new StringBuilder();              
              
              //theo danh muc
              if (DanhMucComboBox.SelectedValue != "-1")
                  whereQuery.AppendFormat(" CatID == \"{0}\" ", DanhMucComboBox.SelectedValue.Trim());

              if (Convert.ToInt32(TrangThaiComboBox.SelectedValue) != 0)
              {
                  if (whereQuery.ToString() != "")
                      whereQuery.AppendFormat(" and State == {0} ", TrangThaiComboBox.SelectedValue.Trim());
                  else
                      whereQuery.AppendFormat(" State == {0} ", TrangThaiComboBox.SelectedValue.Trim());
              }

              var query = from tl in db.TaiLieus select tl;
              if (whereQuery.ToString() != "")
                  query = from tl in db.TaiLieus.Where(whereQuery.ToString()) select tl;
             //kiem tra Sap sep
              switch (Convert.ToInt32(SapSepComBoBox.SelectedValue))
              {                        

                  //mua nhiều nhất
                  case 1:
                      if (whereQuery.ToString() != "")
                          query = from tl in db.TaiLieus.Where(whereQuery.ToString()) orderby tl.NumsBuy descending select tl;
                      else
                          query = from tl in db.TaiLieus  orderby tl.NumsBuy descending select tl;
                      break;

                  //xem nhiều nhất   
                  case 2:
                      if (whereQuery.ToString() != "")
                          query = from tl in db.TaiLieus.Where(whereQuery.ToString()) orderby tl.NumsView descending select tl;
                      else
                          query = from tl in db.TaiLieus orderby tl.NumsView descending select tl;
                      break;

                  //còn hàng
                  case 3:
                      if (whereQuery.ToString() != "")
                          whereQuery.Append(" and ProductQuantity > NumsBuy ");
                      else
                          whereQuery.Append(" ProductQuantity > NumsBuy ");
                      query = from tl in db.TaiLieus.Where(whereQuery.ToString()) select tl;
                      break;

                  //hết hàng
                  case 4:
                      if (whereQuery.ToString() != "")
                          whereQuery.Append(" and ProductQuantity <= NumsBuy ");
                      else
                          whereQuery.Append(" ProductQuantity <= NumsBuy ");
                      query = from tl in db.TaiLieus.Where(whereQuery.ToString()) select tl;
                      break;
                      
              }
    
              //lay thong tin can cho phan trang
              int trang = 1;

              if (Request.QueryString["Page"] != null)
                  trang = Convert.ToInt32(Request.QueryString["Page"].ToString());
              List<TaiLieu> lst = new List<TaiLieu>();
              if (query.Count<TaiLieu>() > 0)
              {
                  int nums = query.Count<TaiLieu>();
                  Session["TongSoDongDuLieu"] = nums;
                  Session["LinkPhanTrang"] = "ProductManager.aspx";
                  int soDongTrenTrang = Convert.ToInt32(Session["SoDongTrenTrang"]);
                  if (soDongTrenTrang <= 0)
                  {
                      soDongTrenTrang = 10;
                      Session["SoDongTrenTrang"] = 10;
                  }
                  int skip = (trang - 1) * soDongTrenTrang;
                  lst = query.Skip(skip).Take(soDongTrenTrang).ToList<TaiLieu>();
              }
              else {
                  Session["TongSoDongDuLieu"] = 0;
                  Session["SoDongTrenTrang"] = 10;
              }
             
                
              
%>
<div class="tab-content default-tab" id="tab1"> 
<!-- This is the target div. id must match the href of this div's tab -->
					
<!-- Start Notification -->
<% if (act != "" && isSuccess)
   {%>
<div class="notification <%if(isSuccess){%>success<%}else{%>attention<%}%> png_bg">
	<a href="javascript:void(0);" class="closeMessage">
        <img src="Images/icons/cross_grey_small.png" title="Đóng thông báo này" alt="Đóng" />
    </a>
	<div><% if (act == "delete")
         {%>
		   Xóa thành công tài liệu
	<%} %></div>
</div>
<%} %>
<!-- End Notification -->
						
	<table align="center" width="100%">	
    						
		<thead>
         <tr>
				<td colspan="10">
					<div class="bulk-actions align-left">	
                      <asp:DropDownList ID="SapSepComBoBox" runat="server"                             
                            AutoPostBack="True">
                          <asp:ListItem Value="0">Tất cả</asp:ListItem>
                          <asp:ListItem Value="1">Mua nhiều nhất</asp:ListItem>
                          <asp:ListItem Value="2">Xem nhiều nhất</asp:ListItem>
                          <asp:ListItem Value="3">Còn hàng</asp:ListItem>
                          <asp:ListItem Value="4">Hết hàng</asp:ListItem>
                        </asp:DropDownList>	
                        <asp:DropDownList ID="TrangThaiComboBox" runat="server"                           
                            AutoPostBack="True"  
                           >
                            <asp:ListItem Value="0">Tất cả</asp:ListItem>
                            <asp:ListItem Selected="True" Value="-1">Chưa giao dịch</asp:ListItem>
                            <asp:ListItem Value="1">Đang giao dịch</asp:ListItem>
                        </asp:DropDownList>	
                        <asp:DropDownList ID="DanhMucComboBox" runat="server"                            
                            AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="CatName" 
                            DataValueField="CatID" 
                            ondatabound="DanhMucComboBox_DataBound">                            
                        </asp:DropDownList>			
                        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                            ContextTypeName="NhaKinhDoanhDataContext" EntityTypeName="" 
                            Select="new (CatID, CatName)" TableName="DanhMucs">
                        </asp:LinqDataSource>
                        <input id="deletehidden" name="delete" type="hidden" value="ProductManager.aspx?act=delete&Page=<%=trang%>"/>
                        <% if (Convert.ToInt32(TrangThaiComboBox.SelectedValue) == -1)
                           { %>
                        <input id="btndelete" type="submit" value="Xóa tài liệu được chọn" class="button" />
                         <%} %>
					</div>
									
				</td>
			</tr>
			<tr>
				<th><input class="check-all" type="checkbox"  name="checkall"/>                    
                </th>
                <th>STT</th>
				<th>Tên</th>
				<th>Giá</th>
				<th>Số lượng</th>			
                <th>Danh Mục</th>
                <th>Xem</th>
                <th>Mua</th>
                <th>Trạng thái</th>
                <th>Tác vụ</th>             
			</tr>
								
		</thead>
				 
		<tfoot>
			<tr>
				<td colspan="10">					
										 
            <!-- Start Pagging -->
            <uc1:Pagging ID="Pagging1" runat="server" />
            <!-- End Pagging -->
									
				</td>
			</tr>
		</tfoot>
						 
		<tbody>
          <% 
              int stt = 0;
          foreach (TaiLieu tl in lst)
	      {
              stt++;
              string trangthaiTaiLieu = "";
              //load tất cả tài liệu
              if(TrangThaiComboBox.SelectedValue=="0")
                  trangthaiTaiLieu= TrangThaiComboBox.Items.FindByValue(tl.State.ToString()).Text;
              else //load tài liệu theo trang thái
                  trangthaiTaiLieu = TrangThaiComboBox.Items.FindByValue(TrangThaiComboBox.SelectedValue).Text;
             
	      %>
			<tr>
				<td><input type="checkbox" name="MaTaiLieu" value="<%=tl.ProductID%>" /></td>	
                <td><%=string.Format("{0:000}",stt)%></td>			
				<td><a href="EditProduct.aspx?act=edit&id=<%=tl.ProductID%>&Page=<%=trang%>" title="<%=tl.ProductDescription %>"><%=MD5Hash.Trim(tl.ProductName)%></a></td>
				<td><%=MD5Hash.Trim(string.Format("{0:0.##}",tl.ProductPrice))%></td>
				<td><%=MD5Hash.Trim(string.Format("{0:0,0}",tl.ProductQuantity))%></td>             
                <td><%=MD5Hash.Trim(tl.DanhMuc.CatName)%></td>    
                <td><%=MD5Hash.Trim(string.Format("{0:0,0}",tl.NumsView))%></td>   
                <td><%=MD5Hash.Trim(string.Format("{0:0,0}",tl.NumsBuy))%></td>                          
                <td><%=MD5Hash.Trim(trangthaiTaiLieu)%></td>     
				<td>					
						<a href="EditProduct.aspx?act=view&id=<%=tl.ProductID%>&Page=<%=trang%>" title="Xem chi tiết"><img src="Images/icons/details.png" alt="Xem chi tiết"/></a>
						<a href="EditProduct.aspx?act=edit&id=<%=tl.ProductID%>&Page=<%=trang%>" title="Cập nhật"><img src="Images/icons/pencil.png" alt="Cập nhật"/></a>
                        <% if (tl.State == -1)
                           { %>
                        <a name="deleteone" href="ProductManager.aspx?act=delete&id=<%=tl.ProductID%>&Page=<%=trang%>" title="Xóa"><img src="Images/icons/cross.png" alt="Xóa" /></a> 		
                        <%} %>				
				</td>
			</tr>								
			
			<%}%>
		</tbody>							
	</table>						
</div>


<!-- End Tab Danh Sach-->					
 
   
					
</div> <!-- End .content-box-content -->
				
</div>

</asp:Content>
